package str;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
 * leet-code(345)  反转字符串中的元音字母
 *
 * @author Ringo
 * @date 2021/8/19 20:46
 */
public class ReverseVowels {

    public static void main(String[] args) {
        String s = "leetcode";
        System.out.println(reverseVowels(s));
    }

    public static String reverseVowels(String s) {
        if (s == null || s.length() == 0)
            return s;
        // 这里是 Character
        Set<Character> set = new HashSet<>(Arrays.asList('a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U'));
        int lo = 0, hi = s.length() - 1;
        char[] chars = s.toCharArray();
        while (lo < hi) {
            if (set.contains(chars[lo]) && set.contains(chars[hi])) {
                // 交换
                char temp = chars[lo];
                chars[lo] = chars[hi];
                chars[hi] = temp;
                ++lo;
                --hi;
            } else if (set.contains(chars[lo]) && !set.contains(chars[hi]))
                --hi;
            else if (!set.contains(chars[lo]) && set.contains(chars[hi]))
                ++lo;
            else {
                ++lo;
                --hi;
            }
        }
        return new String(chars);
    }
}
